package cn.wolfcode.luowowo.website.web.advice;

import cn.wolfcode.luowowo.common.exception.LogicException;
import cn.wolfcode.luowowo.common.util.AjaxResult;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * 统一异常处理
 * @ControllerAdvice
 * Controller类功能增强注解
 * 在请求映射方法处理前或处理后对请求映射昂阿飞做功能性增强
 *
 * 处理前:时间类型参数格式化
 * 处理后:统一异常处理
 * 问题:为什么使用AOP,帮你处理了什么?
 */
@ControllerAdvice
public class CommonExceptionHandler {

    //当抛出的异常时LogicException时,会进入该方法,帮我们进行返回
    @ExceptionHandler(LogicException.class)
    @ResponseBody
    public Object displayExp(Exception e, HttpServletResponse resp) throws IOException {
        e.printStackTrace();
       return new AjaxResult(false,e.getMessage());
    }
    //RuntimeException
    @ExceptionHandler(RuntimeException.class)
    @ResponseBody
    public Object runTimeExp(Exception e, HttpServletResponse resp) throws IOException {
        e.printStackTrace();
       return new AjaxResult(false,"系统繁忙,请联系管理员");
    }

}
